"""
交易 - 通用交易相关模型
"""

from typing import Dict, Any, Optional, List
from jlpay.model.base import OrgBaseReq, BaseResponse


class CancelRequest(OrgBaseReq):
    """订单撤销请求"""
    
    API_URL = "/open/trans/cancel"
    
    def __init__(self):
        """初始化订单撤销请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.total_fee = None  # 交易金额
        self.ori_transaction_id = None  # 原平台订单号
        self.remark = None  # 备注
        self.mch_create_ip = None  # 终端IP
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
    
    def set_ori_out_trade_no(self, ori_out_trade_no: str):
        """设置原外部订单号"""
        self.ori_out_trade_no = ori_out_trade_no
    
    def set_total_fee(self, total_fee: str):
        """设置交易金额"""
        self.total_fee = total_fee
    
    def set_ori_transaction_id(self, ori_transaction_id: str):
        """设置原平台订单号"""
        self.ori_transaction_id = ori_transaction_id
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'total_fee': self.total_fee,
            'ori_transaction_id': self.ori_transaction_id,
            'remark': self.remark,
            'mch_create_ip': self.mch_create_ip,
            'longitude': self.longitude,
            'latitude': self.latitude
        })
        return data


class CancelResponse(BaseResponse):
    """订单撤销响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化订单撤销响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.order_time = None  # 订单时间
        self.pay_type = None  # 交易类型
        self.total_fee = None  # 交易金额
        self.trans_time = None  # 交易时间
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.ori_out_trade_no = response_dict.get('ori_out_trade_no')
            self.ori_transaction_id = response_dict.get('ori_transaction_id')
            self.order_time = response_dict.get('order_time')
            self.pay_type = response_dict.get('pay_type')
            self.total_fee = response_dict.get('total_fee')
            self.trans_time = response_dict.get('trans_time')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'order_time': self.order_time,
            'pay_type': self.pay_type,
            'total_fee': self.total_fee,
            'trans_time': self.trans_time
        })
        return data


class RefundRequest(OrgBaseReq):
    """退款请求"""
    
    API_URL = "/open/trans/refund"
    
    def __init__(self):
        """初始化退款请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.total_fee = None  # 交易金额
        self.refund_fee = None  # 退款金额
        self.ori_transaction_id = None  # 原平台订单号
        self.remark = None  # 备注
        self.mch_create_ip = None  # 终端IP
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
    
    def set_ori_out_trade_no(self, ori_out_trade_no: str):
        """设置原外部订单号"""
        self.ori_out_trade_no = ori_out_trade_no
    
    def set_total_fee(self, total_fee: str):
        """设置交易金额"""
        self.total_fee = total_fee
    
    def set_refund_fee(self, refund_fee: str):
        """设置退款金额"""
        self.refund_fee = refund_fee
    
    def set_ori_transaction_id(self, ori_transaction_id: str):
        """设置原平台订单号"""
        self.ori_transaction_id = ori_transaction_id
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'total_fee': self.total_fee,
            'refund_fee': self.refund_fee,
            'ori_transaction_id': self.ori_transaction_id,
            'remark': self.remark,
            'mch_create_ip': self.mch_create_ip,
            'longitude': self.longitude,
            'latitude': self.latitude
        })
        return data


class RefundResponse(BaseResponse):
    """退款响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化退款响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.total_fee = None  # 交易金额
        self.refund_fee = None  # 退款金额
        self.order_time = None  # 订单时间
        self.pay_type = None  # 交易类型
        self.trans_time = None  # 交易时间
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.ori_out_trade_no = response_dict.get('ori_out_trade_no')
            self.ori_transaction_id = response_dict.get('ori_transaction_id')
            self.total_fee = response_dict.get('total_fee')
            self.refund_fee = response_dict.get('refund_fee')
            self.order_time = response_dict.get('order_time')
            self.pay_type = response_dict.get('pay_type')
            self.trans_time = response_dict.get('trans_time')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'total_fee': self.total_fee,
            'refund_fee': self.refund_fee,
            'order_time': self.order_time,
            'pay_type': self.pay_type,
            'trans_time': self.trans_time
        })
        return data


class GetOpenidRequest(OrgBaseReq):
    """获取OpenID请求"""
    
    API_URL = "/open/trans/getopenid"
    
    def __init__(self):
        """初始化获取OpenID请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.pay_type = None  # 交易类型
        self.auth_code = None  # 授权码
        self.sub_appid = None  # 公众账号ID
        self.app_up_identifier = None  # 银联支付标识
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 终端IP
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
    
    def set_pay_type(self, pay_type: str):
        """设置交易类型"""
        self.pay_type = pay_type
    
    def set_auth_code(self, auth_code: str):
        """设置授权码"""
        self.auth_code = auth_code
    
    def set_sub_appid(self, sub_appid: str):
        """设置公众账号ID"""
        self.sub_appid = sub_appid
    
    def set_app_up_identifier(self, app_up_identifier: str):
        """设置银联支付标识"""
        self.app_up_identifier = app_up_identifier
    
    def set_device_info(self, device_info: str):
        """设置终端设备号"""
        self.device_info = device_info
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'pay_type': self.pay_type,
            'auth_code': self.auth_code,
            'sub_appid': self.sub_appid,
            'app_up_identifier': self.app_up_identifier,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip
        })
        return data


class GetOpenidResponse(BaseResponse):
    """获取OpenID响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化获取OpenID响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.mch_id = None  # 商户号
        self.openid = None  # 用户在商户appid下的唯一标识
        self.sub_openid = None  # 用户在子商户appid下的唯一标识
        
        if response_dict:
            self.mch_id = response_dict.get('mch_id')
            self.openid = response_dict.get('openid')
            self.sub_openid = response_dict.get('sub_openid')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'openid': self.openid,
            'sub_openid': self.sub_openid
        })
        return data


class ChnQueryRequest(OrgBaseReq):
    """渠道查询请求"""

    API_URL = "/open/trans/chnquery"
    
    def __init__(self):
        """初始化渠道查询请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.out_trade_no = None  # 外部订单号
        self.transaction_id = None  # 平台订单号
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
    
    def set_transaction_id(self, transaction_id: str):
        """设置平台订单号"""
        self.transaction_id = transaction_id
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'out_trade_no': self.out_trade_no,
            'transaction_id': self.transaction_id
        })
        return data


class ChnQueryResponse(BaseResponse):
    """渠道查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化渠道查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态
        self.mch_id = None  # 商户号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.total_fee = None  # 交易金额
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.pay_type = None  # 交易类型
        self.chn_pay_type = None  # 渠道支付类型
        self.chn_name = None  # 渠道名称
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.total_fee = response_dict.get('total_fee')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.pay_type = response_dict.get('pay_type')
            self.chn_pay_type = response_dict.get('chn_pay_type')
            self.chn_name = response_dict.get('chn_name')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'total_fee': self.total_fee,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'pay_type': self.pay_type,
            'chn_pay_type': self.chn_pay_type,
            'chn_name': self.chn_name
        })
        return data


class AuthCompleteRefundRequest(OrgBaseReq):
    """预授权完成退款请求"""
    
    API_URL = "/open/trans/authcompleterefund"
    
    def __init__(self):
        """初始化预授权完成退款请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.total_fee = None  # 预授权退款金额
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 终端IP
        self.trans_address = None  # 交易地址
        self.area_code = None  # 交易地区码
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
        self.remark = None  # 备注
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_term_no(self, term_no: str):
        """设置终端号"""
        self.term_no = term_no
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def set_ori_out_trade_no(self, ori_out_trade_no: str):
        """设置原外部订单号"""
        self.ori_out_trade_no = ori_out_trade_no
        return self
    
    def set_ori_transaction_id(self, ori_transaction_id: str):
        """设置原平台订单号"""
        self.ori_transaction_id = ori_transaction_id
        return self
    
    def set_total_fee(self, total_fee: str):
        """设置预授权退款金额"""
        self.total_fee = total_fee
        return self
    
    def set_device_info(self, device_info: str):
        """设置终端设备号"""
        self.device_info = device_info
        return self
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_trans_address(self, trans_address: str):
        """设置交易地址"""
        self.trans_address = trans_address
        return self
    
    def set_area_code(self, area_code: str):
        """设置交易地区码"""
        self.area_code = area_code
        return self
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
        return self
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'total_fee': self.total_fee,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'trans_address': self.trans_address,
            'area_code': self.area_code,
            'longitude': self.longitude,
            'latitude': self.latitude,
            'remark': self.remark
        })
        return data


class AuthCompleteRefundResponse(BaseResponse):
    """预授权完成退款响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化预授权完成退款响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.total_fee = None  # 预授权退款金额
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.pay_type = None  # 交易类型
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.total_fee = response_dict.get('total_fee')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.pay_type = response_dict.get('pay_type')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'total_fee': self.total_fee,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'pay_type': self.pay_type
        })
        return data


class PreAuthCloseRequest(OrgBaseReq):
    """预授权关闭请求"""
    
    API_URL = "/open/trans/preauthclose"
    
    def __init__(self):
        """初始化预授权关闭请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.pay_type = None  # 交易类型
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.guarantee_auth_code = None  # 授权码
        self.total_fee = None  # 关单金额
        self.remark = None  # 备注
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 终端IP
        self.trans_address = None  # 交易地址
        self.area_code = None  # 交易地区码
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_term_no(self, term_no: str):
        """设置终端号"""
        self.term_no = term_no
        return self
    
    def set_pay_type(self, pay_type: str):
        """设置交易类型"""
        self.pay_type = pay_type
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def set_ori_out_trade_no(self, ori_out_trade_no: str):
        """设置原外部订单号"""
        self.ori_out_trade_no = ori_out_trade_no
        return self
    
    def set_ori_transaction_id(self, ori_transaction_id: str):
        """设置原平台订单号"""
        self.ori_transaction_id = ori_transaction_id
        return self
    
    def set_guarantee_auth_code(self, guarantee_auth_code: str):
        """设置授权码"""
        self.guarantee_auth_code = guarantee_auth_code
        return self
    
    def set_total_fee(self, total_fee: str):
        """设置关单金额"""
        self.total_fee = total_fee
        return self
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
        return self
    
    def set_device_info(self, device_info: str):
        """设置终端设备号"""
        self.device_info = device_info
        return self
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_trans_address(self, trans_address: str):
        """设置交易地址"""
        self.trans_address = trans_address
        return self
    
    def set_area_code(self, area_code: str):
        """设置交易地区码"""
        self.area_code = area_code
        return self
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'pay_type': self.pay_type,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'guarantee_auth_code': self.guarantee_auth_code,
            'total_fee': self.total_fee,
            'remark': self.remark,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'trans_address': self.trans_address,
            'area_code': self.area_code,
            'longitude': self.longitude,
            'latitude': self.latitude
        })
        return data


class PreAuthCloseResponse(BaseResponse):
    """预授权关闭响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化预授权关闭响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.pay_type = None  # 交易类型
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.pay_type = response_dict.get('pay_type')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'pay_type': self.pay_type
        })
        return data


class PreAuthCompleteRequest(OrgBaseReq):
    """预授权完成请求"""
    
    API_URL = "/open/trans/preauthcomplete"
    
    def __init__(self):
        """初始化预授权完成请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.pay_type = None  # 交易类型
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.guarantee_auth_code = None  # 授权码
        self.total_fee = None  # 预授权完成金额
        self.remark = None  # 备注
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 终端IP
        self.trans_address = None  # 交易地址
        self.area_code = None  # 交易地区码
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_term_no(self, term_no: str):
        """设置终端号"""
        self.term_no = term_no
        return self
    
    def set_pay_type(self, pay_type: str):
        """设置交易类型"""
        self.pay_type = pay_type
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def set_ori_out_trade_no(self, ori_out_trade_no: str):
        """设置原外部订单号"""
        self.ori_out_trade_no = ori_out_trade_no
        return self
    
    def set_ori_transaction_id(self, ori_transaction_id: str):
        """设置原平台订单号"""
        self.ori_transaction_id = ori_transaction_id
        return self
    
    def set_guarantee_auth_code(self, guarantee_auth_code: str):
        """设置授权码"""
        self.guarantee_auth_code = guarantee_auth_code
        return self
    
    def set_total_fee(self, total_fee: str):
        """设置预授权完成金额"""
        self.total_fee = total_fee
        return self
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
        return self
    
    def set_device_info(self, device_info: str):
        """设置终端设备号"""
        self.device_info = device_info
        return self
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_trans_address(self, trans_address: str):
        """设置交易地址"""
        self.trans_address = trans_address
        return self
    
    def set_area_code(self, area_code: str):
        """设置交易地区码"""
        self.area_code = area_code
        return self
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'pay_type': self.pay_type,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'guarantee_auth_code': self.guarantee_auth_code,
            'total_fee': self.total_fee,
            'remark': self.remark,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'trans_address': self.trans_address,
            'area_code': self.area_code,
            'longitude': self.longitude,
            'latitude': self.latitude
        })
        return data


class PreAuthCompleteResponse(BaseResponse):
    """预授权完成响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化预授权完成响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.total_fee = None  # 预授权交易完成金额
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.pay_type = None  # 交易类型
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.total_fee = response_dict.get('total_fee')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.pay_type = response_dict.get('pay_type')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'total_fee': self.total_fee,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'pay_type': self.pay_type
        })
        return data


class PreAuthRevokeRequest(OrgBaseReq):
    """预授权撤销请求"""
    
    API_URL = "/open/trans/preauthrevoke"
    
    def __init__(self):
        """初始化预授权撤销请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.pay_type = None  # 交易类型
        self.out_trade_no = None  # 外部订单号
        self.ori_out_trade_no = None  # 原外部订单号
        self.ori_transaction_id = None  # 原平台订单号
        self.guarantee_auth_code = None  # 授权码
        self.total_fee = None  # 撤销金额
        self.remark = None  # 备注
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 终端IP
        self.trans_address = None  # 交易地址
        self.area_code = None  # 交易地区码
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_term_no(self, term_no: str):
        """设置终端号"""
        self.term_no = term_no
        return self
    
    def set_pay_type(self, pay_type: str):
        """设置交易类型"""
        self.pay_type = pay_type
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def set_ori_out_trade_no(self, ori_out_trade_no: str):
        """设置原外部订单号"""
        self.ori_out_trade_no = ori_out_trade_no
        return self
    
    def set_ori_transaction_id(self, ori_transaction_id: str):
        """设置原平台订单号"""
        self.ori_transaction_id = ori_transaction_id
        return self
    
    def set_guarantee_auth_code(self, guarantee_auth_code: str):
        """设置授权码"""
        self.guarantee_auth_code = guarantee_auth_code
        return self
    
    def set_total_fee(self, total_fee: str):
        """设置撤销金额"""
        self.total_fee = total_fee
        return self
    
    def set_remark(self, remark: str):
        """设置备注"""
        self.remark = remark
        return self
    
    def set_device_info(self, device_info: str):
        """设置终端设备号"""
        self.device_info = device_info
        return self
    
    def set_mch_create_ip(self, mch_create_ip: str):
        """设置终端IP"""
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_trans_address(self, trans_address: str):
        """设置交易地址"""
        self.trans_address = trans_address
        return self
    
    def set_area_code(self, area_code: str):
        """设置交易地区码"""
        self.area_code = area_code
        return self
    
    def set_longitude(self, longitude: str):
        """设置经度"""
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude: str):
        """设置纬度"""
        self.latitude = latitude
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'pay_type': self.pay_type,
            'out_trade_no': self.out_trade_no,
            'ori_out_trade_no': self.ori_out_trade_no,
            'ori_transaction_id': self.ori_transaction_id,
            'guarantee_auth_code': self.guarantee_auth_code,
            'total_fee': self.total_fee,
            'remark': self.remark,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'trans_address': self.trans_address,
            'area_code': self.area_code,
            'longitude': self.longitude,
            'latitude': self.latitude
        })
        return data


class PreAuthRevokeResponse(BaseResponse):
    """预授权撤销响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化预授权撤销响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.total_fee = None  # 预授权撤销金额
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.pay_type = None  # 交易类型
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.total_fee = response_dict.get('total_fee')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.pay_type = response_dict.get('pay_type')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'total_fee': self.total_fee,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'pay_type': self.pay_type
        })
        return data


class PreAuthQueryRequest(OrgBaseReq):
    """预授权查询请求"""
    
    API_URL = "/open/trans/preauthquery"
    
    def __init__(self):
        """初始化预授权查询请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.out_trade_no = None  # 外部订单号
        self.transaction_id = None  # 平台订单号
    
    def set_mch_id(self, mch_id: str):
        """设置商户号"""
        self.mch_id = mch_id
        return self
    
    def set_out_trade_no(self, out_trade_no: str):
        """设置外部订单号"""
        self.out_trade_no = out_trade_no
        return self
    
    def set_transaction_id(self, transaction_id: str):
        """设置平台订单号"""
        self.transaction_id = transaction_id
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'out_trade_no': self.out_trade_no,
            'transaction_id': self.transaction_id
        })
        return data


class PreAuthQueryResponse(BaseResponse):
    """预授权查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化预授权查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.status = None  # 状态 1：待确认 2：成功 3：失败
        self.mch_id = None  # 商户号
        self.transaction_id = None  # 平台订单号
        self.out_trade_no = None  # 外部订单号
        self.chn_transaction_id = None  # 渠道订单号
        self.guarantee_auth_code = None  # 授权码
        self.total_fee = None  # 交易金额
        self.order_time = None  # 订单时间
        self.trans_time = None  # 交易时间
        self.term_no = None  # 终端号
        self.device_info = None  # 终端设备号
        self.remark = None  # 备注
        self.op_user_id = None  # 操作员
        self.op_shop_id = None  # 门店号
        self.finnal_amount = None  # 实际付款金额
        self.discount_amount = None  # 优惠总金额
        self.discount_name = None  # 优惠活动名称
        self.coupon_info = None  # 优惠信息
        self.pay_type = None  # 交易类型
        self.sub_openid = None  # 用户openid
        
        if response_dict:
            self.status = response_dict.get('status')
            self.mch_id = response_dict.get('mch_id')
            self.transaction_id = response_dict.get('transaction_id')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.chn_transaction_id = response_dict.get('chn_transaction_id')
            self.guarantee_auth_code = response_dict.get('guarantee_auth_code')
            self.total_fee = response_dict.get('total_fee')
            self.order_time = response_dict.get('order_time')
            self.trans_time = response_dict.get('trans_time')
            self.term_no = response_dict.get('term_no')
            self.device_info = response_dict.get('device_info')
            self.remark = response_dict.get('remark')
            self.op_user_id = response_dict.get('op_user_id')
            self.op_shop_id = response_dict.get('op_shop_id')
            self.finnal_amount = response_dict.get('finnal_amount')
            self.discount_amount = response_dict.get('discount_amount')
            self.discount_name = response_dict.get('discount_name')
            self.coupon_info = response_dict.get('coupon_info')
            self.pay_type = response_dict.get('pay_type')
            self.sub_openid = response_dict.get('sub_openid')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'status': self.status,
            'mch_id': self.mch_id,
            'transaction_id': self.transaction_id,
            'out_trade_no': self.out_trade_no,
            'chn_transaction_id': self.chn_transaction_id,
            'guarantee_auth_code': self.guarantee_auth_code,
            'total_fee': self.total_fee,
            'order_time': self.order_time,
            'trans_time': self.trans_time,
            'term_no': self.term_no,
            'device_info': self.device_info,
            'remark': self.remark,
            'op_user_id': self.op_user_id,
            'op_shop_id': self.op_shop_id,
            'finnal_amount': self.finnal_amount,
            'discount_amount': self.discount_amount,
            'discount_name': self.discount_name,
            'coupon_info': self.coupon_info,
            'pay_type': self.pay_type,
            'sub_openid': self.sub_openid
        })
        return data 